/*
 * Render.cpp
 *
 *  Created on: Nov 19, 2013
 *      Author: axel
 */

#include "Render.h"
#include "extraMath.h"
#include "SimInfList.h"

namespace render {

void drawRectArr(Vector3f* ver, GLuint* ind) {
	GLuint vbo, vbo2;
	glGenBuffers(1, &vbo);
	glGenBuffers(1, &vbo2);

	Vector3f Vertices[4];
	for(int i=0;i<4;i++)
		Vertices[i] = ver[i];

	GLuint Indices[6];
	for(int i=0;i<6;i++)
		Indices[i] = ind[i];

	glEnableVertexAttribArray(0);

	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER,sizeof(Vertices), Vertices, GL_DYNAMIC_DRAW);

	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo2);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(Indices), Indices, GL_DYNAMIC_DRAW);

	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

	glDisableVertexAttribArray(0);

	glDeleteBuffers(1, &vbo);
	glDeleteBuffers(1, &vbo2);
}

//Rendering to draw a circle
void drawCircle(double x, double y, double width) {
	GLuint vbo, vbo2;
	glGenBuffers(1, &vbo);
	glGenBuffers(1, &vbo2);

	double angle=0;
	int index=1;
	static const int triangles = 100;
	Vector3f Vertices[triangles];
	Vertices[0] = Vector3f(x, y, 0.0f);

	for(int i =1;i<triangles;i++) {
		Vertices[i]=Vector3f(x+width*math::cos_axel(angle), y+width*math::sin_axel(angle), 0.0f);
		angle += (360.0/(triangles)) * PI / 180.0;
	}

	GLuint Indices[(triangles)*3];
	for(int i=0;i<(triangles)*3;i+=3) {
		Indices[i]= 0;
		Indices[i+1] = index;
		Indices[i+2] = index+1;
		if (index+1 == triangles)
			Indices[i+2] = 1;
		index++;
	}

	glEnableVertexAttribArray(0);

	glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER,sizeof(Vertices), Vertices, GL_DYNAMIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo2);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(Indices), Indices, GL_DYNAMIC_DRAW);

    glDrawElements(GL_TRIANGLES, (triangles)*3, GL_UNSIGNED_INT, 0);

	glDisableVertexAttribArray(0);

	glDeleteBuffers(1, &vbo);
	glDeleteBuffers(1, &vbo2);

}
void drawCube(Vector3f* vertices,GLuint* indices, GLfloat* color) {
	GLuint vbo,vbo2,vbo3;
	glGenBuffers(1, &vbo);
	glGenBuffers(1, &vbo2);
	glGenBuffers(1, &vbo3);

	Vector3f Vertices[8];
	for(int i=0;i<8;i++) {
		Vertices[i]= vertices[i];
	}

	GLuint Indices[36];
	for(int i=0;i<36;i++) {
		Indices[i]= indices[i];
	}

	GLfloat Color[108];
	for(int i=0;i<108;i++) {
		Color[i]= color[i];
	}

	glEnableVertexAttribArray(1);

	glBindBuffer(GL_ARRAY_BUFFER, vbo3);
	glBufferData(GL_ARRAY_BUFFER, sizeof(Color), Color, GL_STATIC_DRAW);

	glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,0);

	glDisableVertexAttribArray(0);

	glEnableVertexAttribArray(0);

	glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER,sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);


	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo2);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(Indices), Indices, GL_STATIC_DRAW);

    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);

	glDisableVertexAttribArray(0);

	glDeleteBuffers(1, &vbo);
	glDeleteBuffers(1, &vbo2);
	glDeleteBuffers(1, &vbo3);
}

void drawRect(double x, double y, double width, double length) {
	GLuint vbo, vbo2;
	glGenBuffers(1, &vbo);
	glGenBuffers(1, &vbo2);

	Vector3f Vertices[4];
    Vertices[0] = Vector3f(x,y, 0.0f);
    Vertices[1] = Vector3f(x-width,y+length, 0.0f);
    Vertices[2] = Vector3f(x-width,y, 0.0f);
    Vertices[3] = Vector3f(x,y+length, 0.0f);

    GLuint Indices[] = { 2, 3, 1,
                         0, 3, 2};

	glEnableVertexAttribArray(0);

	glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER,sizeof(Vertices), Vertices, GL_DYNAMIC_DRAW);

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo2);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(Indices), Indices, GL_DYNAMIC_DRAW);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

	glDisableVertexAttribArray(0);

	glDeleteBuffers(1, &vbo);
	glDeleteBuffers(1, &vbo2);
}

}
